<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/27 0027
 * Time: 0:09
 */

namespace app\storage\model;


use think\Model;

class SeatMachineModel extends Model
{
    protected $table = 'smt_seat_machine';
    protected $pk = 'uuid';

    /***
     * @param $condition
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取某个机台的站位（展示）
     */
    public function getSeat($condition){
        $where = $condition['where'];
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        $ret['total'] = $this->where($where)->count();
        $ret['row'] = $this
            ->where($where)->order('seat_code asc')
            ->page($pageIndex,$pageSize)
            ->select();
        return $ret;
    }

    /***
     * @param $condition
     * @return mixed
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * 获取正在使用的某个机台的站位（展示）
     */
    public function getMachineSeatByIsUsed($condition){
        $where = $condition['where'];
        $where['is_used']=1;
        $pageIndex = $condition['pageIndex'];
        $pageSize = $condition['pageSize'];
        $ret['total'] = $this->where($where)->count();
        $ret['row'] = $this
            ->where($where)->order('seat_code asc')
            ->page($pageIndex,$pageSize)
            ->select();
        return $ret;
    }


    /***
     * @param $data
     * @return int|string
     * 添加机台站位
     */
    public function addMachineSeat($data){

        $machineUuid = $data['uuid'];//机台的uuid
        $machineId = $data['machine_id'];//机台的Id
        $machineStorage = $data['storage_uuid'];
        $seatNumber = $data['seatNumber'];//机台上站位的数目
        $uuid = create_uuid();//创建站位的uuid
        $seatDatas = array(array('uuid' => $uuid, 'machine_uuid' => $machineUuid, 'machine_id' => $machineId, 'seat_code' => 1, 'is_used' => 0, 'sub_materiel_id' => '', 'status' => 1 ,'storage_uuid' => $machineStorage));
        for($i = 2;$i <= $seatNumber;$i++){
            $uuid = create_uuid();
                $seatDatas = array_merge($seatDatas,array(array('uuid' => $uuid, 'machine_uuid' => $machineUuid, 'machine_id' => $machineId, 'seat_code' => $i, 'is_used' => 0, 'sub_materiel_id' => '', 'status' => 1, 'storage_uuid' => $machineStorage)));
        }//生成站位,seat_code从1开始
        $result = $this->insertAll($seatDatas);//插入站位

        return $result;
    }

    /***
     * @param $where
     * @return int
     * 根据传入的条件删除符合条件的机台站位。
     */
    public function deleteSeat($where){
        $result = $this->where($where)->delete();
        return $result;
    }
}